package hasthi.wsdm;

import static hasthi.common.constants.WSDMConstants.HOSTNAME_PARAM;
import static hasthi.common.constants.WSDMConstants.MUWS2_NAMESPACE;
import static hasthi.common.constants.WSDMConstants.RESOURCE_CATEGORY_PARAM;
import static hasthi.common.constants.WSDMConstants.RESOURCE_TYPE_PARAM;
import static hasthi.common.constants.WSDMConstants.SERVICE_NAME;
import hasthi.common.Utils;
import hasthi.metamodel.resource.ManagedResource;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.namespace.QName;

import xsul.wsdl.WsdlDefinitions;
import xsul.wsdl.WsdlPortType;
import xsul.wsdl.WsdlService;

public class ManagementAgentUtils {
    public static Map<QName, Object> createServiceManagementProperties(WsdlDefinitions wsdl) {
       
        
        QName serviceName = null;
        Iterator it;
        WsdlService service = null;

        QName serviceType = null;
        if (wsdl != null && wsdl.getServices() != null) {
            it = wsdl.getServices().iterator();
            if (it.hasNext()) {
                service = (WsdlService) it.next();
            }
            if (serviceName == null) {
                serviceName = new QName(wsdl.getTargetNamespace(), service.getAttributeValue(
                        null, "name"));
            }
            if (wsdl.getPortTypes() != null) {
                it = wsdl.getPortTypes().iterator();
                if (it.hasNext()) {
                    WsdlPortType port = (WsdlPortType) it.next();
                    serviceType = new QName(wsdl.getTargetNamespace(), port.getAttributeValue(
                            null, "name"));
                }
            }
        }

        // TODO may be we should parse the WSDL

        if (service == null) {
            serviceName = new QName("http://extreme.indiana.edu/services", service.getName());
        }
        if (serviceType == null) {
            serviceType = serviceName;
        }
        return createServiceManagementProperties(serviceName, serviceType);
    }     
        
   public static Map<QName, Object> createServiceManagementProperties(QName serviceName,QName serviceType) {  
        Map<QName, Object> properties = new HashMap<QName, Object>();
        String descrption = serviceName + " Service";
        properties.put(new QName(SERVICE_NAME), serviceName.toString());
        properties.put(new QName(MUWS2_NAMESPACE, "Caption"), serviceName.getLocalPart());
        properties.put(RESOURCE_CATEGORY_PARAM, ManagedResource.ResourceType.Service.toString());
        properties.put(RESOURCE_TYPE_PARAM, serviceType.toString());
        properties.put(HOSTNAME_PARAM, Utils.getLocalHost());
        properties.put(new QName(MUWS2_NAMESPACE, "Description"), descrption);
        return properties;
    }

}
